package com.publicstaticdroid.g2l_interpreter_connect;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * This class intercepts Global2Local interpretation requests.
 * @author Nathan Breit
 *
 */
public class SMSRequestReceiver extends SMSReceiver
{

	@Override
	protected void recieveRequest(Context c, String phoneNumber, int requestID, String language) {
		SharedPreferences settings = c.getSharedPreferences(Constants.PREFS_KEY, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("requestsRecieved", 1 + settings.getInt("requestsRecieved", 0));
		editor.commit();

		
		// Android doesn't want you to make dialogs appear from the background
		// and instead use notifications,
		// but we do it anyways to make the app easier to use.
		Intent needInterpreterIntent = new Intent(c, NeedAnInterpreter.class);
		//Question: Is there a flag that will finish any old instance of NeedAnInterpreter and create a new one?
		needInterpreterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		needInterpreterIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);

		needInterpreterIntent.putExtra("language", language);
		needInterpreterIntent.putExtra("requestID", requestID);
		//TODO: See if I can somehow avoid launching in front of calls
		c.startActivity(needInterpreterIntent);
		
		//makeNotification(c, phoneNumber, requestID, language);
	}
	@Override
	protected void dismissRequest(Context c, String phoneNumber, int requestID) {
		// Display Toast notification that someone else handled this request
		//String noticeString = "Someone else has answered that interpreting request!";
		//Toast.makeText(c, noticeString, Toast.LENGTH_LONG).show();
		// cancel the corresponding NeedAnInterpreter.class accept/reject dialog, if it exists
		// commented out because it (1) didn't actually work and (2) needlessly brought the app to the front
		//					Intent cancelRequest = new Intent(currentContext, NeedAnInterpreter.class);
		//					cancelRequest.putExtra("cancel", true);
		//					cancelRequest.putExtra("requestID", words[1]);
		//					cancelRequest.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//					currentContext.startActivity(cancelRequest);

		//Nathan: Do we need to cancel the accept/reject dialog?
		//		  I'm guessing this in an issue only when the user has already opened the notification.
		
		Log.i(LOG_TAG, "SMSRequestReceiver dismiss command for rid: " + requestID);
		
		//TODO: Potential bug where NeedInterp activity is gone
		Intent cancelRequest = new Intent(c, NeedAnInterpreter.class);
		cancelRequest.putExtra("cancel", true);
		cancelRequest.putExtra("requestID", requestID);
		cancelRequest.addFlags(Intent.FLAG_FROM_BACKGROUND);
		cancelRequest.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);//Do I need this one?
		c.startActivity(cancelRequest);
	}
	@Override
	protected void callCommand(Context c, String phoneNumber, int requestID, String phoneNumberToCall) {
		Log.e(LOG_TAG, "SMSRequestReceiver got call command. " +
				"To act on call commands register the SMSCallReceiver with a higher priority than the SMSRequestReceiver." +
				"See MakeCallActivity for an example of how to do it.");
	}
	/**
	 * @deprecated
	 * Creates a status bar Notification (along with vibration, sound, and lights) alerting the 
	 * interpreter of an (urgent) incoming interpreting request.
	 * 
	 * @param currentContext	the Context in which the SMSReceiver is running
	 * @param sender				the sender (system server)'s phone number [or email address]
	 */
	private void makeNotification(Context currentContext, String sender, int requestID, String language)
	{
		// "You cannot launch a popup dialog in your implementation of onReceive()"
		// OK; but we can raise a fairly insistent notification that prompts the interpreter
		// to start an Activity containing a popup dialog to get the interpreter's response


		NotificationManager notifier;
		notifier = (NotificationManager) currentContext.getSystemService(Context.NOTIFICATION_SERVICE);

		// components of a new Notification
		int icon = android.R.drawable.stat_notify_error; // TODO: create R.drawable.notification_icon
		long now = System.currentTimeMillis();
		CharSequence tickerText = "G2L " + language + " interpreter needed now!";
		CharSequence notifyTitle = "G2L " + language + " interpreter needed";
		CharSequence notifyText = "Please accept this request if you can.";

		// Intent that answering the notification will trigger
		// Question: Why do intents take the .class of the activity rather than an instance?
		//           And is there a trade off between passing args as extras rather than instantiating with them?
		Intent askForInterpreter = new Intent(currentContext, NeedAnInterpreter.class);
		askForInterpreter.putExtra("replyTo", sender);
		askForInterpreter.putExtra("requestID", requestID);
		askForInterpreter.putExtra("language", language);
		askForInterpreter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		// PendingIntent wrapper for the askForInterpreter Intent
		PendingIntent notify = PendingIntent.getActivity(currentContext, requestID, 
				askForInterpreter, PendingIntent.FLAG_UPDATE_CURRENT);

		// Notification and its parameters (sound, vibration, etc.)
		Notification notification = new Notification(icon, tickerText, now);
		notification.setLatestEventInfo(currentContext, notifyTitle, notifyText, notify);
		long[] vibrate = { 0, 1000, 2500, 500, 4000 };	// buzz pattern: off, on, off, on, off (in ms)
		notification.vibrate = vibrate;
		notification.defaults |= Notification.DEFAULT_SOUND;
		notification.flags |= Notification.FLAG_INSISTENT;
		notification.ledARGB = 0xff00ff00;
		notification.ledOnMS = 300;
		notification.ledOffMS = 1000;
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;		

		// Tag each notification with the relevant interpreting requestID
		notifier.notify(requestID, notification);

		Log.i(LOG_TAG, "Made notification for request id " + requestID + " (" + language + ")");
	}

}
